#!/bin/sh

os=$(uname -s)

if [[ "$os" == "Darwin" ]]; then
    host_tag="darwin-x86_64"
elif [[ "$os" == "CYGWIN"* ]]; then
    host_tag="windows-x86_64"
else
    host_tag="linux-x86_64"
fi

sysroot="${ANDROID_NDK_ROOT}"/toolchains/llvm/prebuilt/${host_tag}/sysroot/
[ ! -d "$sysroot" ] && echo "Android sysroot $sysroot does not exist!" && exit 1

function generate() {
	if [ -z "$1" ]; then
		echo "call without arg"
		return
	fi
	bindgen $sysroot/usr/include/$1.h -o src/$1.rs \
		--rustfmt-configuration-file "${PWD}/rustfmt.toml"\
		--no-layout-tests \
		--merge-extern-blocks \
		--no-prepend-enum-name \
		--no-recursive-allowlist \
		--rustified-enum "AHardwareBuffer_UsageFlags" \
		--allowlist-file ".*$1.h" -- \
		--sysroot="$sysroot" --target=aarch64-linux-android
}

function generate_recursive() {
	if [ -z "$1" ]; then
		echo "call without arg"
		return
	fi
	bindgen $sysroot/usr/include/$1.h -o src/$1.rs \
		--rustfmt-configuration-file "${PWD}/rustfmt.toml"\
		--no-layout-tests \
		--merge-extern-blocks \
		--no-prepend-enum-name \
		--allowlist-file ".*$1.h" -- \
		--sysroot="$sysroot" --target=aarch64-linux-android
}

function generate_plus() {
	if [ -z "$1" ]; then
		echo "call without arg"
		return
	fi
	bindgen $sysroot/usr/include/$1.h -o src/$1.rs \
		--rustfmt-configuration-file "${PWD}/rustfmt.toml"\
		--no-layout-tests \
		--merge-extern-blocks \
		--no-prepend-enum-name \
		--no-recursive-allowlist \
		--allowlist-file ".*$1.h" -- \
		--sysroot="$sysroot" --target=aarch64-linux-android -x c++
}

if [[ "$1" = "audio" ]]; then
	generate_recursive "aaudio/AAudio"
fi

if [[ "$1" = "midi" ]]; then
	generate "amidi/AMidi"
fi

if [[ "$1" = "media" ]]; then
	while read FILE; do
		echo "media/$FILE"
		generate "media/$FILE"
	done << EOF
NdkImage
NdkMediaCodec
NdkMediaDataSource
NdkMediaError
NdkMediaFormat
NdkImageReader
NdkMediaCrypto 
NdkMediaDrm   
NdkMediaExtractor
NdkMediaMuxer
EOF
fi

if [[ "$1" = "camera" ]]; then
	while read FILE; do
		echo "camera/$FILE"
		generate "camera/$FILE"
	done << EOF
NdkCameraCaptureSession
NdkCameraError
NdkCameraMetadata
NdkCameraWindowType
NdkCameraDevice
NdkCameraManager
NdkCameraMetadataTags
NdkCaptureRequest
EOF
fi

if [[ "$1" = "android" ]]; then
	while read FILE; do
		echo "android/$FILE"
		generate "android/$FILE"
	done << EOF
NeuralNetworks
NeuralNetworksTypes
api-level
asset_manager
asset_manager_jni
bitmap
choreographer
configuration
data_space
dlext
fdsan
file_descriptor_jni
hardware_buffer
hardware_buffer_jni
hdr_metadata
imagedecoder
input
keycodes
log
looper
native_activity
native_window
native_window_jni
ndk-version
obb
performance_hint
rect
sensor
set_abort_message
sharedmem
sharedmem_jni
storage_manager
surface_texture
surface_texture_jni
trace
versioning
window
EOF

	while read FILE; do
		echo "android/$FILE"
		generate_recursive "android/$FILE"
	done << EOF
multinetwork
permission_manager
sync
EOF

	while read FILE; do
		echo "android/$FILE"
		generate_plus "android/$FILE"
	done << EOF
surface_control
thermal
EOF

echo "font.h"
bindgen font.h -o src/android/font.rs \
	--rustfmt-configuration-file "${PWD}/rustfmt.toml"\
	--no-layout-tests \
	--merge-extern-blocks \
	--no-prepend-enum-name \
	--no-recursive-allowlist \
	--allowlist-file ".*font.h" \
	--allowlist-file ".*font_matcher.h" \
	--allowlist-file ".*system_fonts.h" \
	-- \
	--sysroot="$sysroot" --target=aarch64-linux-android -x c++

fi

# if [[ "$1" = "ax" ]]; then

# binder_auto_utils
# binder_enums
# binder_ibinder
# binder_ibinder_jni
# binder_interface_utils
# binder_internal_logging
# binder_parcel
# binder_parcel_jni
# binder_parcel_utils
# binder_parcelable_utils
# binder_status
# binder_to_string

# fi

# if [[ "$1" = "androidx" ]]; then
# 	for file in `ls $sysroot/usr/include/android/*.h`; do
# 				# echo "$file"
# 		if test -f $file; then
# 			# generate "android/$file"
# 			#  | tr '[:upper:]' '[:lower:]'
# 			name=`basename -s .h "$file"`
# 			# generate "android/$name"
# 			echo "$name"
# 		fi
# 		if test -d $file; then
# 			echo "$file"
# 		fi
# 	done
# fi


# legacy_errno_inlines
# legacy_fenv_inlines_arm
# legacy_signal_inlines
# legacy_stdlib_inlines
# legacy_sys_mman_inlines
# legacy_sys_stat_inlines
# legacy_sys_statvfs_inlines
# legacy_sys_wait_inlines
# legacy_termios_inlines
# legacy_threads_inlines
# legacy_unistd_inlines
